﻿using System;
using Capture.MME;
using Transport;
namespace Capture
{
    /// <summary>
    /// MmeCapturer
    /// </summary>
    public class MmeCapturer : WaveCapturer, ICapturer
    {
        private IAudioTransport _encoder;

        public MmeCapturer(IAudioTransport encoder)
        {
            if (encoder == null)
                throw new ArgumentNullException("encoder");
            _encoder = encoder;
        }
        public override void Dispose()
        {
            base.Dispose();
            _encoder.Dispose();
        }

        public static bool IsSupported
        {
            get { return (DeviceInfo.GetDevices() > 0); }
        }

        public void Initialize()
        {
            //BehaviorVector[(int)CapDevice.BehaviorVectorMask.DispatchOnWorkerThread] = false;
        }

        public void Start()
        {
            Start(() => new MmeWaveInfo(this));
        }

        /// <summary>
        /// MmeWaveInfo
        /// </summary>
        private class MmeWaveInfo : WaveInfo
        {
            private IAudioTransport _encoder;

            public MmeWaveInfo(MmeCapturer parent)
            {
                _encoder = parent._encoder;
            }

            public override void NewWave(IntPtr buffer, int bufferLen)
            {
                base.NewWave(buffer, bufferLen);
                _encoder.EncodeAndTransmit(buffer, bufferLen);
                //Console.Write(".");
            }

            public override void SetProperties(Native_.WaveFormat header)
            {
                base.SetProperties(header);
                //Console.WriteLine("PropertyChangesDone");
            }
        }
    }
}
